<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use Workerman\Worker;
 
//Autoloader.php路径
require_once "Workerman/Autoloader.php";

class Socket {
	public function __construct() {
		$this->CI= &get_instance();
        // 加载数据库类
        $this->CI->load->model('classesModel');
    }

    // 发送紧急通知
    public function send_urgent_msg($schoolId, $classIdSet, $notifyId){
    	// 获取班牌列表
    	$brandParmas['schoolId'] = $schoolId;
    	$brandParmas['classIdSet'] = $classIdSet;
    	$brandParmas['online'] = 1;
    	$brandList = $this->CI->classesModel->get_brand_list($brandParmas);
    	// 遍历目标对象，分别推送消息
    	foreach($brandList as $brand) {
    		$msgData = array(
                "id" => $notifyId,
    			"type" => CONTENT_TYPE_URGENT
    		);
    		$this->send_channel_msg($brand['id'], $msgData);
    	}
    }
    // 发送班级通知
    public function send_notify_msg($schoolId, $classIdSet, $notifyId){
        // 获取班牌列表
        $brandParmas['schoolId'] = $schoolId;
        $brandParmas['classIdSet'] = $classIdSet;
        $brandParmas['online'] = 1;
        $brandList = $this->CI->classesModel->get_brand_list($brandParmas);
        // 遍历目标对象，分别推送消息
        foreach($brandList as $brand) {
            $msgData = array(
                "id" => $notifyId,
                "type" => CONTENT_TYPE_NOTIFY
            );
            $this->send_channel_msg($brand['id'], $msgData);
        }
    }
    // 发送班级通知
    public function send_news_msg($schoolId, $classIdSet, $newsId){
        // 获取班牌列表
        $brandParmas['schoolId'] = $schoolId;
        $brandParmas['classIdSet'] = $classIdSet;
        $brandParmas['online'] = 1;
        $brandList = $this->CI->classesModel->get_brand_list($brandParmas);
        // 遍历目标对象，分别推送消息
        foreach($brandList as $brand) {
            $msgData = array(
                "id" => $newsId,
                "type" => CONTENT_TYPE_NEWS
            );
            $this->send_channel_msg($brand['id'], $msgData);
        }
    }

    // 发送作业通知
    public function send_homework_msg($schoolId, $classIdSet, $homeworkId){
        // 获取班牌列表
        $brandParmas['schoolId'] = $schoolId;
        $brandParmas['classIdSet'] = $classIdSet;
        $brandParmas['online'] = 1;
        $brandList = $this->CI->classesModel->get_brand_list($brandParmas);
        // var_export($brandList);exit;
        // 遍历目标对象，分别推送消息
        foreach($brandList as $brand) {
            $msgData = array(
                "id" => $homeworkId,
                "type" => CONTENT_TYPE_HOMEWORK
            );
            $this->send_channel_msg($brand['id'], $msgData);
        }
    }

    // 发送校园公告通知
    public function send_announce_msg($schoolId, $classIdSet, $announceId){
        // 获取班牌列表
        $brandParmas['schoolId'] = $schoolId;
        $brandParmas['classIdSet'] = $classIdSet;
        $brandParmas['online'] = 1;
        $brandList = $this->CI->classesModel->get_brand_list($brandParmas);
        // 遍历目标对象，分别推送消息
        foreach($brandList as $brand) {
            $msgData = array(
                "id" => $announceId,
                "type" => CONTENT_TYPE_ANNOUNCE
            );
            $this->send_channel_msg($brand['id'], $msgData);
        }
    }

    // 更新学生信息
    public function update_student_list($schoolId, $classIdSet, $studentId){
        // 获取班牌列表
        $brandParmas['schoolId'] = $schoolId;
        $brandParmas['classIdSet'] = $classIdSet;
        $brandParmas['online'] = 1;
        $brandList = $this->CI->classesModel->get_brand_list($brandParmas);
        // 遍历目标对象，分别推送消息
        foreach($brandList as $brand) {
            $msgData = array(
                "id" => $studentId,
                "type" => CONTENT_TYPE_STUDENT
            );
            $this->send_channel_msg($brand['id'], $msgData);
        }
    }

    // 推送实时截图消息
    public function send_screen_shot_msg($brandId, $channelId){
         $msgData = array(
            "id" => $channelId,
            "type" => CONTENT_TYPE_SCREENSHOT
        );
        $this->send_channel_msg($brandId, $msgData);
    }

    // 推送实时截图消息
    public function send_quit_msg($brandId, $channelId){
        $msgData = array(
            "id" => $channelId,
            "type" => CONTENT_TYPE_QUIT
        );
        $this->send_channel_msg($brandId, $msgData);
    }

    // 推送远程关机消息
    public function send_power_off_msg($brandId, $channelId){
         $msgData = array(
            "id" => $channelId,
            "type" => CONTENT_TYPE_POWEROFF
        );
        $this->send_channel_msg($brandId, $msgData);
    }

    // 推送远程重启消息
    public function send_power_restart_msg($brandId, $channelId){
        $msgData = array(
           "id" => $channelId,
           "type" => CONTENT_TYPE_POWERRESTART
       );
       $this->send_channel_msg($brandId, $msgData);
   }
   
    // 推送定时开关机消息
    public function send_power_time_msg($brandId, $powerTime, $week, $holiday){
         $msgData = array(
            "time" => $powerTime,
            "week" => $week,
            "holiday" => $holiday,
            "type" => CONTENT_TYPE_POWERTIME
        );
        // echo '<pre>';var_export($msgData);exit;
        $this->send_channel_msg($brandId, $msgData);
    }

    // 推送定时开关机消息
    public function send_power_time_v2_msg($brandId, $powerTimeV2){
        $msgData = array(
            "time" => $powerTimeV2,
            "type" => CONTENT_TYPE_POWERTIME_V2
        );
        // echo '<pre>';var_export($msgData);exit;
        $this->send_channel_msg($brandId, $msgData);
    }

    // 推送获取版本消息
    public function send_get_version_msg($brandId, $channelId){
         $msgData = array(
            "id" => $channelId,
            "type" => CONTENT_TYPE_GETVERSION
        );
        $this->send_channel_msg($brandId, $msgData);
    }

    // 推送班牌app更新提醒
    public function send_update_version_msg($brandId, $channelId){
         $msgData = array(
            "id" => $channelId,
            "type" => CONTENT_TYPE_UPDATEVERSION
        );
        $this->send_channel_msg($brandId, $msgData);
    }

    // 推送获取本地考勤数据消息
    public function send_get_sign_data_msg($brandId, $channelId){
         $msgData = array(
            "id" => $channelId,
            "type" => CONTENT_TYPE_SIGN_DATA
        );
        $this->send_channel_msg($brandId, $msgData);
    }

    // 推送班牌留言消息
    public function send_brand_note_msg($brandId, $noteId, $noteType, $noteContent, $studentId, $studentName, $otherUserType, $otherUserId, $createtime){
        $msgData = array(
            "type" => CONTENT_TYPE_BRAND_NOTE,
            "note_id" => $noteId,
            "note_type" => $noteType,
            "note_content" => $noteContent,
            "student_id" => $studentId,
            "student_name" => $studentName,
            'other_user_type' => $otherUserType,
            'other_user_id' => $otherUserId,
            'createtime' => $createtime
        );
        $this->send_channel_msg($brandId, $msgData);
    }

    // 推送撤回班牌留言消息
    public function send_brand_note_delete_msg($brandId, $noteId){
        $msgData = array(
            "type" => CONTENT_TYPE_BRAND_NOTE_DELETE,
            "note_id" => $noteId,
        );
        $this->send_channel_msg($brandId, $msgData);
    }

    // 推送收到对话留言消息
    public function send_note_msg($channelId, $noteId, $noteType, $noteContent, $headimgurl, $isBrand, $createtime){
        $msgData = array(
            "type" => CONTENT_TYPE_NOTE,
            "note_id" => $noteId,
            "note_type" => $noteType,
            "note_content" => $noteContent,
            'headimgurl' => $headimgurl,
            'is_brand' => $isBrand,
            'createtime' => $createtime
        );
        $this->send_channel_msg($channelId, $msgData);
    }

    // 推送撤回对话留言消息
    public function send_note_delete_msg($channelId, $noteId, $isBrand){
        $msgData = array(
            "type" => CONTENT_TYPE_NOTE_DELETE,
            "note_id" => $noteId,
            'is_brand' => $isBrand
        );
        $this->send_channel_msg($channelId, $msgData);
    }

    // 推送收到对话留言消息，用于列表页
    public function send_note_msg_for_list($channelId, $userId, $content, $isBrand, $createtime){
        $msgData = array(
            "type" => CONTENT_TYPE_NOTE,
            "user_id" => $userId,
            "content" => $content,
            'is_brand' => $isBrand,
            'createtime' => $createtime
        );
        $this->send_channel_msg($channelId, $msgData);
    }

    // 推送撤回对话留言消息
    public function send_note_delete_msg_for_list($channelId, $userId, $isBrand, $createtime){
        $msgData = array(
            "type" => CONTENT_TYPE_NOTE_DELETE,
            "user_id" => $userId,
            "is_brand" => $isBrand,
            "createtime" => $createtime
        );
        $this->send_channel_msg($channelId, $msgData);
    }

    // 推送对话留言消息已读
    public function send_note_msg_read($channelId, $noteId, $isBrand){
        $msgData = array(
            "type" => CONTENT_TYPE_NOTE_READ,
            "note_id" => $noteId,
            'is_brand' => $isBrand
        );
        $this->send_channel_msg($channelId, $msgData);
    }

    // 推送对话留言消息已读
    public function send_note_msg_read_audio($channelId, $noteId, $isBrand){
        $msgData = array(
            "type" => CONTENT_TYPE_NOTE_READ_AUDIO,
            "note_id" => $noteId,
            'is_brand' => $isBrand
        );
        $this->send_channel_msg($channelId, $msgData);
    }

    // 推送班牌留言消息已读
    public function send_brand_note_msg_read($channelId, $studentId, $otherUserType, $otherUserId){
        $msgData = array(
            "type" => CONTENT_TYPE_BRAND_NOTE_READ,
            "student_id" => $studentId,
            'other_user_type' => $otherUserType,
            'other_user_id' => $otherUserId
        );
        $this->send_channel_msg($channelId, $msgData);
    }

    // 推送修改学生人脸
    public function send_face_image_update($brandId, $studentId, $faceimg){
        $msgData = array(
            "type" => CONTENT_TYPE_UPDATE_FACE,
            "student_id" => $studentId,
            "faceimg" => $faceimg
        );
        $this->send_channel_msg($brandId, $msgData);
    }

    // 发送紧急通知
    public function send_channel_msg($channelId, $dataArr){

        // 建立socket连接到内部推送端口
        $client = stream_socket_client('tcp://127.0.0.1:5678', $errno, $errmsg, 1);
        // var_export($client);exit;
        // 推送的数据，包含uid字段，表示是给这个uid推送
        // $data = array('channel_id'=>'10112', 'percent'=>'91%');
        
        $channelArr = array('channel_id' => $channelId);
        $msgArr = array_merge($channelArr, $dataArr);
        // 发送数据，注意5678端口是Text协议的端口，Text协议需要在数据末尾加上换行符
        fwrite($client, json_encode($msgArr)."\n");
        // // 读取推送结果
        // echo fread($client, 8192);
    }
}